import sys
import pygame
import screen
import highscore_screen
import phoenix
import cloud

class GameScreen(screen.Screen):
    def __init__(self):
        screen.Screen.__init__(self)

        self.game_screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT), pygame.DOUBLEBUF|pygame.HWSURFACE)

        self.width = self.game_screen.get_width()
        self.height = self.game_screen.get_height()
        self.interval = 0.001
        self.num_enemies = 13

        self.phoenix = phoenix.Phoenix(self.width, self.height)
        self.group = pygame.sprite.Group()
        self.group.add(self.phoenix)

        for _ in range(self.num_enemies):
            self.group.add(cloud.Cloud(self.width, self.height))
            
        self.leftover = 0.0
        self.clock = pygame.time.Clock();
        self.font = pygame.font.Font(None, 24)

    def render(self):
        self.game_screen.fill((0, 0, 0))
        self.group.draw(self.game_screen)

        surf = self.font.render("FPS: %f" % (self.clock.get_fps()), True, (255, 255, 255))
        self.game_screen.blit(surf, (24, 24))
        self.clock.tick()

    def update(self, time):
        self.group.update(time)

    def process_events(self, change_state):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == pygame.K_LEFT:
                    self.phoenix.set_x_velocity(-200)
                elif event.key == pygame.K_RIGHT:
                    self.phoenix.set_x_velocity(200)
                elif event.key == pygame.K_UP:
                    self.phoenix.set_y_velocity(-200)
                elif event.key == pygame.K_DOWN:
                    self.phoenix.set_y_velocity(200)
                elif event.key == pygame.K_RETURN:
                    change_state(highscore_screen.HighScoreScreen())

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    self.phoenix.set_x_velocity(0)
                elif event.key == pygame.K_RIGHT:
                    self.phoenix.set_x_velocity(0)
                elif event.key == pygame.K_UP:
                    self.phoenix.set_y_velocity(0)
                elif event.key == pygame.K_DOWN:
                    self.phoenix.set_y_velocity(0)
